//
//  BattleInputLayer.m
//  SpaceStorm
//
//  Created by 祥 雍 on 13-5-9.
//  Copyright (c) 2013年 __MyCompanyName__. All rights reserved.
//

#import "BattleInputLayer.h"
#import "BattleScene.h"
#import "Variable.h"

@implementation BattleInputLayer

-(id) init
{
    // always call "super" init
	// Apple recommends to re-assign "self" with the "super" return value
	if( (self=[super init])) 
    {
        // ask director the the window size
		size = [[CCDirector sharedDirector] winSize];
        
        UIPanGestureRecognizer *gestureRecognizer1 = [[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanFrom:)] autorelease];
        [[[CCDirector sharedDirector] openGLView] addGestureRecognizer:gestureRecognizer1];
        
        UIPinchGestureRecognizer *gestureRecognizer = [[[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlePinchFrom:)] autorelease];    //1
        [[[CCDirector sharedDirector] openGLView] addGestureRecognizer:gestureRecognizer]; //2
        lastScale = 1.f; //3
    }
    
	return self;
}

/**
	处理2指捏合
	@param recognizer 
 */
-(void) handlePinchFrom:(UIPinchGestureRecognizer*)recognizer
{
    if([recognizer state] == UIGestureRecognizerStateEnded)
    {    
        
        if(recognizer.scale < 1)//缩小，战争视图升一个等级，最高到全局视图
        {
            if (viewType < BattleViewMax) 
            {
                viewType ++;
                [[BattleScene sharedLayer] updateViewType:viewType];
                CCLOG(@"视图升级!");
            }
            else
            {
                CCLOG(@"视图已到最高等级:全局视图，不能再升了!");
            }
        }
        else if(recognizer.scale > 1)//放大，战争视图降一个等级，最低到微观视图
        {
            if (viewType > BattleViewMicroscopic) 
            {
                viewType --;
                [[BattleScene sharedLayer] updateViewType:viewType];
                CCLOG(@"视图降级!");
            }
            else
            {
                CCLOG(@"视图已到最低等级:微观视图，不能再降了!");
            }
        }

    }
    
    
//    if([recognizer state] == UIGestureRecognizerStateBegan)
//    {    
//        
//        lastScale = [[BattleScene sharedLayer] backGroundLayer].scale;
//    }
//    float nowScale;    
//    nowScale = (lastScale - 1) + recognizer.scale;    
//    nowScale = MIN(nowScale,2);//设置缩放上限
//    nowScale = MAX(nowScale,1);//设置缩放下限   
//    
//    allowRect = [self rectOfPositionAllow];
//    
//    if (lastScale > nowScale)
//    {
//        
//        CGPoint newPosition =  ccpSub(self.position, ccpMult ( ccpNormalize(self.position) ,ccpLength(self.position) *(lastScale - nowScale)/(lastScale - 1))) ;
//        if (CGRectContainsPoint(allowRect, newPosition))
//        {
//            self.position = newPosition;
//        }
//    }
//    [[BattleScene sharedLayer] backGroundLayer].scale = nowScale;
}

/**
	处理手指滑动
	@param recognizer 
 */
-(void) handlePanFrom:(UIPanGestureRecognizer *)recognizer
{
    
    if (recognizer.state == UIGestureRecognizerStateBegan) 
    {  
        lastPosition = self.position;
        
    } 
    else if (recognizer.state == UIGestureRecognizerStateChanged) 
    { 
        
        CGPoint translation = [recognizer translationInView:recognizer.view];
        translation = ccp(translation.x, -translation.y);
        translation = ccpMult(translation, 0.7f);
        CGPoint newPos = ccpAdd(lastPosition, translation);
//        CCLOG(@"坐标x:%f_y:%f", newPos.x, newPos.y);
        
        //设置 camera的视图中心
        [[BattleScene sharedLayer].camera setViewpointCenter:ccp((int)translation.x, (int)translation.y)];
        
        //加入允许的范围的判断
//        if (CGRectContainsPoint(allowRect, newPos))
//        {
//            self.position = newPos;
//        }       
    } 
}

-(CGRect) rectOfPositionAllow
{
    CGRect theRect;
    theRect.origin.x = size.width - self.boundingBox.size.width;
    theRect.origin.y = size.height - self.boundingBox.size.height;
    theRect.size.width = abs(size.width - self.boundingBox.size.width);
    theRect.size.height = abs(size.height - self.boundingBox.size.height);
    return theRect;
}
@end
